//
//  HOMyViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOMyViewController.h"
#import "HOUtils.h"
#import "HOLoginViewController.h"
#import "HOConstants.h"
#import "HOUIMgr.h"
#import "HOButton.h"
#import "HOConstants.h"
#import "HOUserInfoViewController.h"
#import "HOUserInfoDataModule.h"
#import "Logger.h"
#import "HOHeaderImgView.h"
#import "HODefaultImage.h"
#import "HOTableViewCell.h"
#import "RatingBar.h"
#import "HOUIMgr.h"
#import "MyAppriseListViewController.h"
#import "HOImageMgr.h"
#import "HOMyReportViewController.h"
#import "HOAboutViewController.h"
#import "HOPWDSettingsViewController.h"
#import "HOMessageViewController.h"
#import "HOPatrolViewController.h"
#import "HORecordMeterViewController.h"
#import "HOReportListViewController.h"
#import "HOEditInfoViewController.h"
#import "HOStaticDataMgr.h"
#import "HOMeterViewController.h"
#import "GTMBase64.h"
#import "UIImageView+WebCache.h"
#import "HOContactUsViewController.h"
#import "HOAnalyzeViewController.h"
#import "HOWebViewController.h"
#import "HOQuestionListViewController.h"
#import "HOMyExtraAssistantViewController.h"

static BOOL                isPopedUpRemindDialog;
@interface HOMyViewController()<UIAlertViewDelegate, UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
	UIImageView		*headerImgView;
	UILabel				*departmentLbl;
	UILabel				*nameLbl;
	UILabel				*mobileLbl;
	UIImageView			*genderImgView;
	BOOL 				needDisplayRemindDialog;
	BOOL				viewDidAppear;
	BOOL				dontNeedRequest;
}
@property(nonatomic ,retain)UIActionSheet *actionSheetImpl;
@property(nonatomic, retain)UITapGestureRecognizer *tapForSheet;
@property(nonatomic, retain)UIImage *defaultImg;
@property(nonatomic, retain)UIImageView 		*unreadMsgImgView;

@end

@implementation HOMyViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTableView];
	[self requestUserData];
	[HOStaticDataMgr sharedInstance];
	[self initSubviews];
	
//	[self initPickerView];
//	
//	[self registerTapGesture];
	

	if (IS_MORETHAN_IOS11) {
//		baseTableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
	} else {
		self.automaticallyAdjustsScrollViewInsets = NO;
	}
//	self.navigationController.view.layer.borderWidth = 3;
//	self.navigationController.view.layer.borderColor = [UIColor redColor].CGColor;
	
	// 解决方法三：添加以下代码关闭估算行高
//	baseTableview.estimatedRowHeight = 0;
}

- (void)initSubviews
{
	baseTableview.tableFooterView = [self getFooterViewWithTitle:NSLocalizedString(@"退出登录", @"退出登录")];
	
	self.unreadMsgImgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_news"]] autorelease];
	CGFloat singleWidth = self.view.frame.size.width / 4;
	self.unreadMsgImgView.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x + self.tabBarController.tabBar.frame.size.width - singleWidth / 2 + 4, 5,  self.unreadMsgImgView.frame.size.width, self.unreadMsgImgView.frame.size.height);
	[self.tabBarController.tabBar addSubview:self.unreadMsgImgView];
//	self.unreadMsgImgView.layer.borderWidth = 1;

}


#pragma mark
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//	    [self showPickerView:NO];
//}
//-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
//{
//	return 1;
//}
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
//{
//	return 2;
//}
//
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//	if(row == 0)
//	{
//		return @"庆春院区";
//	}
//	return @"下沙院区";
//}
//
//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
//{
//	pickerViewSelectRow = row;
//}
//
//- (void)didPickDone:(id)sender
//{
//	NSString *companyId = kQingChun;
//	if(pickerViewSelectRow == 2)
//	{
//		companyId = kQingChun;
//	}
//	else
//	{
//		companyId = kXiasha;
//	}
//	[super didPickDone:sender];
////	[[HOStaticDataMgr sharedInstance] getCompanyId];
//
//}
#pragma mark


- (void)didClickFooterButton:(UIButton *)btn
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_Logout}];
	[[HOUIMgr sharedInstance] logout];
}

- (void)viewWillAppear:(BOOL)animated
{
	[self setNavigationItemTitle: NSLocalizedString(@"我的", @"我的")];
//	[self.navigationController.navigationBar setHidden:YES];
	self.tabBarController.tabBar.hidden = NO;
	self.navigationItem.leftBarButtonItem = nil;
	self.navigationItem.hidesBackButton = YES;
	self.userInfo = [[HOStaticDataMgr sharedInstance] getUserInfo];
//	self.userInfo.userType = HO_USER_TYPE_REPAIRER;
	
    [baseTableview reloadData];

    [super viewWillAppear:animated];
//	[self completeInfoRemind];

	[self requestUserData];
}

- (void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
	viewDidAppear = YES;
	if([HOStaticDataMgr sharedInstance].needGotoMyReport)
	{
		[HOStaticDataMgr sharedInstance].needGotoMyReport = NO;
		[self gotoMyReport];
	}
	if([HOStaticDataMgr sharedInstance].needGotoMyMessage)
	{
		[HOStaticDataMgr sharedInstance].needGotoMyMessage = NO;
		[self gotoMyMessage];
	}
}

- (void)viewWillDisappear:(BOOL)animated
{
	[HOStaticDataMgr sharedInstance].needGotoMyReport = NO;
	[super viewWillDisappear:animated];
//	[self.navigationController.navigationBar setHidden:NO];
	[self showPickerView:NO];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
	viewDidAppear = NO;
}

- (void)requestUserData
{
	if(dontNeedRequest)
		return;
//	dontNeedRequest = YES;
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetUser}];
}

- (BOOL)prefersStatusBarHidden
{
	return NO;
}

- (UIView *)getHeaderView
{
	UIView *headerView = [[[UIView alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  self.view.frame.size.width,
											  90)] autorelease];
	//---- Header ImageView
	headerImgView = [[UIImageView alloc]init];
	headerImgView.frame = CGRectMake(HO_HORIZONAL_PADDING,
									 HO_HORIZONAL_PADDING,
									 70,
									 70);
	headerImgView.layer.cornerRadius = 35;
	[headerImgView.layer setMasksToBounds:YES];
	self.defaultImg = [UIImage imageNamed:@"morentu"];
	headerImgView.image = self.defaultImg;
	[headerView addSubview:headerImgView];
//	baseTableview.tableHeaderView = headerView;
	
	CGFloat x = 20 + headerImgView.frame.size.width + headerImgView.frame.origin.x;
	//---- Level UILabel
	
	
	//---- Name UILabel
	nameLbl = [[UILabel alloc]initWithFrame:CGRectMake(x,
													   headerImgView.frame.origin.y + 6,
													   self.view.frame.size.width - x - 80,
													   22)];
//	nameLbl.layer.borderWidth = 3;
	nameLbl.textColor = TEXT_COLOR_181818;
	nameLbl.font = [UIFont systemFontOfSize:15];
	[headerView addSubview:nameLbl];
	
	departmentLbl = [[UILabel alloc]initWithFrame:CGRectMake(x,
														nameLbl.frame.origin.y + 27,
														self.view.frame.size.width - x - HO_HORIZONAL_PADDING,
														22)];
	[headerView addSubview:departmentLbl];
//	[departmentLbl setBackgroundColor:UIColorFromRGB(0xec6084)];
	departmentLbl.layer.cornerRadius = 5;
	departmentLbl.font = [UIFont systemFontOfSize:12];
	departmentLbl.textColor = TEXT_COLOR_999999;
	return headerView;
}

- (void)viewProfile
{
}

- (void)completeInfoRemind
{
	if([HOUIMgr sharedInstance].currentScreen != self)
	{
		needDisplayRemindDialog = NO;
		return;
	}
	if(self.userInfo.userType == HO_USER_TYPE_LEADER)
	{
		needDisplayRemindDialog = NO;
		return;
	}
	if(!self.userInfo.isGotInfo)
	{
		needDisplayRemindDialog = NO;
		return;
	}
	if(self.userInfo.todoFillInfo == 0)
	{
		needDisplayRemindDialog = NO;
		return;
	}
	if(isPopedUpRemindDialog)
	{
		needDisplayRemindDialog = NO;
		isPopedUpRemindDialog = YES;
		return;
	}
	if(viewDidAppear)
	{
		[self popupRemindDialog];
	}
	else
	{
		needDisplayRemindDialog = YES;
	}
}

- (void)popupRemindDialog
{

//	if(isPopedUpRemindDialog)
//		return;
//	isPopedUpRemindDialog = YES;
	
	HOAlertView *alert = [[HOAlertView alloc] initWithTitle:nil
													message:NSLocalizedString(@"您当前还有资料未完善，请继续完善", @"您当前还有资料未完善，请继续完善")
												   delegate:self
										  cancelButtonTitle:NSLocalizedString(@"关闭", @"关闭")
										  otherButtonTitles:NSLocalizedString(@"去完善", @"去完善"), nil];
	alert.tag = [kHO_DIALOG_NeedFillInInfo integerValue];
	[alert show];
	[alert release];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		if(indexPath.row == 0)
		{
			return 90;
		}
		else
		{
			return CELL_HEIGHT;
		}
	}
	if(([self.userInfo isRepairUser] && indexPath.section == 0)
		|| ([self.userInfo isLeaderUser] && indexPath.section == 0))
		return 56;
	if(([self.userInfo isLeaderUser] || [self.userInfo isRepairUser]) && indexPath.section == 1)
	{
		return 58;
	}

	return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	if(self.userInfo.userType == HO_USER_TYPE_LEADER)
		return 4;
	else if(self.userInfo.userType == HO_USER_TYPE_REPORTER)
		return 3;
	else if(self.userInfo.userType == HO_USER_TYPE_REPAIRER)
		return 5;
	return 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(self.userInfo.userType == HO_USER_TYPE_LEADER)
	{
		if(section == 0)
		{
			return 1;
		}
		else if(section == 1)
		{
			return 1;
		}
		else if(section == 2)
		{
			
			return 3;
		}
		else if(section == 3)
		{
			return 4;
		}
	}
	else if(self.userInfo.userType == HO_USER_TYPE_REPORTER)
	{
		if(section == 0)
		{
			return 1;
		}
		else if(section == 1)
		{
			return 2;
		}
		else if(section == 2)
		{
			return 4;
		}
	}
	else if(self.userInfo.userType == HO_USER_TYPE_REPAIRER)
	{
		if(section == 0)
		{
			return 2;
		}
		else if(section == 1)
		{
			return 1;
		}
		else if(section == 2)
		{
			return 7;
		}
		else if(section == 3)
		{
			return 2;
		}
		else
		{
			return 4;
		}
	}
	return 0;
}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 12)] autorelease];
//    [view setBackgroundColor:[UIColor clearColor]];
//    return view;
//}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	
	if(self.userInfo.userType == HO_USER_TYPE_LEADER)
	{
		return [HOUtils defaultOnePixelConversion];
	}
	else
	{
		if(section == 0)
		{
			return [HOUtils defaultOnePixelConversion];
		}
		
		return SECTION_PADDING / 2;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	if(indexPath.section == 0)
	{
		if(indexPath.row == 0)
		{
			static NSString *AMBaseTableViewCellIdentifier = @"UITableViewCell";
			UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
			if(cell == nil)
			{
				cell = [[[UITableViewCell alloc]
						 initWithStyle:UITableViewCellStyleDefault
						 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
			}
			UIView *view = (UIView *)[cell viewWithTag:115];
			if(view == nil)
			{
				view = [self getHeaderView];
				view.tag = 115;
				[cell.contentView addSubview:view];
			}
			if([self.userInfo isLeaderUser])
			{
				nameLbl.text = [NSString stringWithFormat:@"%@-%@", [HOUtils getValideString:self.userInfo.realName], [HOUtils getValideString:self.userInfo.jobName]];
			}
			else
			{
				nameLbl.text = [NSString stringWithFormat:@"%@-%@", [HOUtils getValideString:self.userInfo.realName], [HOUtils getValideString:self.userInfo.jobName]];
				
				//			nameLbl.text = [HOUtils getValideString:self.userInfo.realName];
			}
			
			departmentLbl.text = [HOUtils getValideString:self.userInfo.departmentName];
			
			[headerImgView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.headPic] placeholderImage:self.defaultImg];
			return cell;
		}
		else
		{
			static NSString *UIPushTableViewCell = @"UIPushTableViewCell";
			UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:UIPushTableViewCell];
			//			if(cell == nil)
			{
				cell = [[[UITableViewCell alloc]
						 initWithStyle:UITableViewCellStyleDefault
						 reuseIdentifier: UIPushTableViewCell] autorelease];
				cell.textLabel.font = [UIFont systemFontOfSize:14];
			}
			UISwitch *switchView = (UISwitch *)[cell viewWithTag:119];
			if(switchView == nil)
			{
				switchView = [[[UISwitch alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 70,
																		 (CELL_HEIGHT - 32) / 2,
																		 52,
																		 32)] autorelease];
				switchView.tag = 119;
				[switchView addTarget:self action:@selector(didChangeSwitch:) forControlEvents:UIControlEventValueChanged];
				[cell.contentView addSubview:switchView];
			}
			[switchView setOn:self.userInfo.isWorking];
			cell.textLabel.text = @"工作状态:";
			return cell;
		}
	}
	else
	{

		if(([self.userInfo isRepairUser] && indexPath.section == 1)
		   || ([self.userInfo isLeaderUser] && indexPath.section == 1))
		{
			if(([self.userInfo isRepairUser] && indexPath.row == 0)
			   || ([self.userInfo isLeaderUser] && indexPath.row == 0))
			{
				static NSString *UIBaseTableViewCell = @"UIBaseTableViewCell";
				UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:UIBaseTableViewCell];
				if(cell == nil)
				{
					cell = [[[UITableViewCell alloc]
							 initWithStyle:UITableViewCellStyleDefault
							 reuseIdentifier: UIBaseTableViewCell] autorelease];
				}
				cell.accessoryType = UITableViewCellAccessoryNone;
				CGFloat width = tableView.frame.size.width / 4;
				[self getLineWithTag:665 withCell:cell withX:width withWidth:width];
				[self getLineWithTag:666 withCell:cell withX:width * 2 withWidth:width];
				[self getLineWithTag:667 withCell:cell withX:width * 3 withWidth:width];
				
				UILabel *workLoadLbl = [self getLabelWithTag:1112 withCell:cell];
				UILabel *resTimeLbl = [self getLabelWithTag:1113 withCell:cell];
				UILabel *rateLbl = [self getLabelWithTag:1114 withCell:cell];
				UILabel *repireRateLbl = [self getLabelWithTag:1115 withCell:cell];
				
				workLoadLbl.textColor = TEXT_COLOR_999999;
				resTimeLbl.textColor = TEXT_COLOR_999999;
				rateLbl.textColor = TEXT_COLOR_999999;
				repireRateLbl.textColor = TEXT_COLOR_999999;
				
				workLoadLbl.frame = CGRectMake(0, 8, width, 22);
				resTimeLbl.frame = CGRectMake(width, 8, width, 22);
				rateLbl.frame = CGRectMake(2 * width, 8, width, 22);
				repireRateLbl.frame = CGRectMake(3 * width, 8, width, 22);
				
				workLoadLbl.text = [NSString stringWithFormat:@"%d", self.userInfo.workNums];
				resTimeLbl.text = self.userInfo.speed;
				rateLbl.text = self.userInfo.praise;
				repireRateLbl.text = self.userInfo.repair;
				
				
				UILabel *workLoadTitleLbl = [self getLabelWithTag:112 withCell:cell];
				UILabel *resTimeTitleLbl = [self getLabelWithTag:113 withCell:cell];
				UILabel *rateTitleLbl = [self getLabelWithTag:114 withCell:cell];
				UILabel *repireRateTitleLbl = [self getLabelWithTag:115 withCell:cell];
				
				workLoadTitleLbl.frame = CGRectMake(0, 25, width, 22);
				resTimeTitleLbl.frame = CGRectMake(width, 25, width, 22);
				rateTitleLbl.frame = CGRectMake(2 * width, 25, width, 22);
				repireRateTitleLbl.frame = CGRectMake(3 * width, 25, width, 22);
				workLoadTitleLbl.text = NSLocalizedString(@"本月工作量", @"本月工作量");
				resTimeTitleLbl.text = NSLocalizedString(@"平均维修时间", @"平均维修时间");
				rateTitleLbl.text = NSLocalizedString(@"好评率", @"好评率");
				repireRateTitleLbl.text = NSLocalizedString(@"返修率", @"返修率");
				return cell;
			}
		}
		else
		{
			HOEditTableViewCell *cell = (HOEditTableViewCell *)[super tableView:tableView cellForEditRowAtIndexPath:indexPath];
			[cell initViewsWithHeight:cellHeight withWidth:tableView.frame.size.width];
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
			[cell setTextEditing:NO];
			[cell createCountLbl];
			[cell getRatingView].hidden = YES;
			[cell getDetailLbl].hidden = YES;
			[cell getRatingView].hidden = YES;
			[cell showCountLbl:NO];
			//	[cell setNeedNext:YES];
			[cell getContentLbl].textColor = TEXT_COLOR_181818;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			if(([self.userInfo isReportUser] && indexPath.section == 1)
			   || ([self.userInfo isRepairUser] && indexPath.section == 2)
			   || ([self.userInfo isLeaderUser] && indexPath.section == 2))
			{
				if([self.userInfo isLeaderUser] && indexPath.row == 0)
				{
					[cell setIconWithName:@"ic_wode_chakanbaobiao"];
					[cell setContent:NSLocalizedString(@"查看报表", @"查看报表")];
				}
				else if([self.userInfo isRepairUser] && indexPath.row == 0)
				{
					[cell setIconWithName:@"ic_wode_weishoulidebaoxiu"];
					[cell setContent:NSLocalizedString(@"未受理的报修单", @"未受理的报修单")];
					[cell setCount:self.userInfo.unacceptTotalCount];
					[cell showCountLbl:self.userInfo.unacceptTotalCount > 0];
					[cell setDetailContent:[NSString stringWithFormat:@"%d", self.userInfo.unhandleReportNumber]];
				}
				
				else if([self.userInfo isRepairUser] && indexPath.row == 1)
				{
					[cell setIconWithName:@"wode_yishouli"];
					[cell setCount:self.userInfo.acceptedTotalCount];
					[cell showCountLbl:self.userInfo.acceptedTotalCount > 0];
					[cell setContent:NSLocalizedString(@"已受理的维修单", @"已受理的维修单")];
				}
				
				
				else if([self.userInfo isRepairUser] && indexPath.row == 2)
				{
					[cell setIconWithName:@"wode_yiwancheng"];
					[cell setContent:NSLocalizedString(@"已完成的维修单", @"已完成")];
				}
				
				else if(([self.userInfo isReportUser] && indexPath.row == 0)
						|| ([self.userInfo isRepairUser] && indexPath.row == 3)
						|| ([self.userInfo isLeaderUser] && indexPath.row == 1))
				{
					[cell setIconWithName:@"ic_wode_wodebaoxiu"];
					[cell setContent:NSLocalizedString(@"我的报修单", @"我的报修单")];
				}
				
				else if([self.userInfo isRepairUser] && indexPath.row == 4)
				{
					[cell setIconWithName:@"ic_wode_wodebaoxiu"];
					[cell setContent:NSLocalizedString(@"我的协助单", @"我的协助单")];
					[cell setCount:self.userInfo.acceptAssistantTotalCount];
					[cell showCountLbl:self.userInfo.acceptAssistantTotalCount > 0];
				}
				else if([self.userInfo isRepairUser] && indexPath.row == 5)
				{
					[cell setIconWithName:@"ic_wode_weishoulidebaoxiu"];
					[cell setContent:NSLocalizedString(@"未受理的协助单", @"未受理的协助单")];
					[cell setCount:self.userInfo.acceptAssistantTotalCount];
					[cell showCountLbl:self.userInfo.acceptAssistantTotalCount > 0];
				}
//				else if(([self.userInfo isRepairUser] && indexPath.row == 5)
//						|| ([self.userInfo isLeaderUser] && indexPath.row == 2))
//				{
//					[cell setIconWithName:@"ic_wode_wodebaoxiu"];
//					[cell setContent:NSLocalizedString(@"我的外协单", @"我的外协单")];
////					[cell setCount:self.userInfo.acceptAssistantTotalCount];
////					[cell showCountLbl:self.userInfo.acceptAssistantTotalCount > 0];
//				}
				else if(([self.userInfo isReportUser] && indexPath.row == 1)
						|| ([self.userInfo isRepairUser] && indexPath.row == 6)
						|| ([self.userInfo isLeaderUser] && indexPath.row == 2))
				{
					[cell setIconWithName:@"ic_wode_wodexiaoxi"];
					[cell setContent:NSLocalizedString(@"我的消息", @"我的消息")];
				}
			}
			else if([self.userInfo isRepairUser] && indexPath.section == 3)
			{
				if(indexPath.row == 0)
				{
					[cell setIconWithName:@"ic_wode_xuncha"];
					[cell setContent:NSLocalizedString(@"巡查", @"巡查")];
				}
				else if(indexPath.row == 1)
				{
					[cell setIconWithName:@"ic_wode_chaobiao"];
					[cell setContent:NSLocalizedString(@"抄表", @"抄表")];
				}	}
			if(([self.userInfo isReportUser] && indexPath.section == 2)
			   || ([self.userInfo isRepairUser] && indexPath.section == 4)
			   || ([self.userInfo isLeaderUser] && indexPath.section == 3))
			{
				if(indexPath.row == 0)
				{
					[cell setIconWithName:@"ic_wode_xiugaimima"];
					[cell setContent:NSLocalizedString(@"修改密码", @"余额")];
				}
				else if(indexPath.row == 1)
				{
					[cell setIconWithName:@"ic_wode_lianxiwomen"];
					[cell setContent:NSLocalizedString(@"联系我们", @"联系我们")];
				}
				else if(indexPath.row == 2)
				{
					[cell setIconWithName:@"ic_my_question"];
					[cell setContent:NSLocalizedString(@"一问一答", @"一问一答")];
				}
				else if(indexPath.row == 3)
				{
					[cell setIconWithName:@"ic_wode_guanyu"];
					[cell setContent:NSLocalizedString(@"关于", @"关于")];
				}
			}
			
			return cell;
		}
		
	}
	
	return nil;
}

- (UILabel *)getLabelWithTag:(NSInteger)tag withCell:(UITableViewCell *)cell
{
	UILabel *lbl = (UILabel *)[cell viewWithTag:tag];
	if(lbl == nil)
	{
		lbl = [[[UILabel alloc] init] autorelease];
		lbl.tag = tag;
		[cell.contentView addSubview:lbl];
		lbl.textAlignment = NSTextAlignmentCenter;
		lbl.textColor = TEXT_COLOR_181818;
		lbl.font = [UIFont systemFontOfSize:12];
	}
	return lbl;
}


- (UIView *)getLineWithTag:(NSInteger)tag withCell:(UITableViewCell *)cell withX:(CGFloat)x withWidth:(CGFloat)width
{
	UIView *lineView = (UILabel *)[cell viewWithTag:tag];
	if(lineView == nil)
	{
		
		lineView = [[[UIView alloc]
							initWithFrame:CGRectMake(x,
													 0,
													 1,
													 58)] autorelease];
		[lineView setBackgroundColor:UIColorFromRGB(0xcdcdcd)];
		[cell.contentView addSubview:lineView];
	}
	return lineView;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
	if(indexPath.section == 0)
	{
		[self takePhoto:nil];
	}
	else if(([self.userInfo isReportUser] && indexPath.section == 1)
			|| ([self.userInfo isRepairUser] && indexPath.section == 2)
			|| ([self.userInfo isLeaderUser] && indexPath.section == 2))
	{
		if([self.userInfo isLeaderUser] && indexPath.row == 0)
		{
			//"查看报表"
			HOAnalyzeViewController *viewCtrl = [[HOAnalyzeViewController alloc] init];
			viewCtrl.displayBackBtn = YES;
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
		if(([self.userInfo isRepairUser] && indexPath.row == 0))
		{
			//"未受理的报修"
			
			HOReportListViewController *viewCtrl = [[HOReportListViewController alloc] init];
			viewCtrl.displayBackBtn = YES;
			viewCtrl.navTitle = NSLocalizedString(@"未受理的报修", @"未受理的报修");
			viewCtrl.currentStatus = HO_REPORT_ORDER_UNACCEPT;
			viewCtrl.isForSingleStatus = YES;
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		
		}
		
		if(([self.userInfo isRepairUser] && indexPath.row == 1))
		{
			//"已受理的维修单"
			
			[self goAcceptList];
			return;
		}
		
		if(([self.userInfo isRepairUser] && indexPath.row == 2))
		{
			//"已经完成的维修单"
			HOReportListViewController *viewCtrl = [[HOReportListViewController alloc] init];
			viewCtrl.displayBackBtn = YES;
			viewCtrl.isForSingleStatus = YES;
			viewCtrl.currentStatus = HO_REPORT_ORDER_REPAIRED;
			viewCtrl.navTitle = NSLocalizedString(@"已经完成的维修单", @"已经完成的维修单");
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
		
		else if(([self.userInfo isReportUser] && indexPath.row == 0)
				|| ([self.userInfo isRepairUser] && indexPath.row == 3)
				|| ([self.userInfo isLeaderUser] && indexPath.row == 1))
		{
			//我的报修
			[self gotoMyReport];
			return;
		}
		else if([self.userInfo isRepairUser] && indexPath.row == 4)
		{
			//我的协助
			[self gotoMyAssistantReport];
			return;
		}
		else if([self.userInfo isRepairUser] && indexPath.row == 5)
		{
			//未受理的的协助
			[self gotoUnacceptAssistantReport];
			return;
		}
//		else if(([self.userInfo isRepairUser] && indexPath.row == 5)
//				|| ([self.userInfo isLeaderUser] && indexPath.row == 2))
//		{
//			//我的外协
//			[self gotoMyExtraAssistantReport];
//			return;
//		}
		else if(([self.userInfo isReportUser] && indexPath.row == 1)
				|| ([self.userInfo isRepairUser] && indexPath.row == 6)
				|| ([self.userInfo isLeaderUser] && indexPath.row == 2))
		{
			//我的消息
			HOMessageViewController *viewCtrl = [[HOMessageViewController alloc] initWithModule:HO_MODULE_TYPE_MESSAGE];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
	}
	else if([self.userInfo isRepairUser] && indexPath.section == 3)
	{
		if(indexPath.row == 0)
		{
			//巡查
			HOPatrolViewController *viewCtrl = [[HOPatrolViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
		else if(indexPath.row == 1)
		{
			//抄表
			HOMeterViewController *viewCtrl = [[HOMeterViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
	}
	if(([self.userInfo isReportUser] && indexPath.section == 2)
	   || ([self.userInfo isRepairUser] && indexPath.section == 4)
	   || ([self.userInfo isLeaderUser] && indexPath.section == 3))
	{
		if(indexPath.row == 0)
		{
			//修改密码
			HOPWDSettingsViewController *viewCtrl = [[HOPWDSettingsViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
		else if(indexPath.row == 1)
		{
			HOContactUsViewController *viewCtrl = [[HOContactUsViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
		}
		else if(indexPath.row == 2)
		{
//			//关于
//			HOWebViewController *viewCtrl = [[HOWebViewController alloc] init];
//			viewCtrl.hideNavigationBar = YES;
//			viewCtrl.loadURL = @"http://115.28.185.111:81/Api/Notice/GetQuestionList?Token=afc8ec467759d9e7e12b9e1bdcb4104e_00001&type=1";
//			[self.navigationController pushViewController:viewCtrl animated:YES];
//			[viewCtrl release];
//			return;
			HOQuestionListViewController *viewCtrl = [[HOQuestionListViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			
		}
		else if(indexPath.row == 3)
		{
			//关于
			HOAboutViewController *viewCtrl = [[HOAboutViewController alloc] init];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			return;
			
		}
	}

}

- (void)goAcceptList
{
	HOReportListViewController *viewCtrl = [[HOReportListViewController alloc] init];
	viewCtrl.displayBackBtn = YES;
	viewCtrl.isForSingleStatus = YES;
	viewCtrl.navTitle = NSLocalizedString(@"已受理的维修单", @"已受理的维修单");
	viewCtrl.currentStatus = HO_REPORT_ORDER_ACCEPTED;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
	return;
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
	// JS url加载完成后，可直接调用JS方法showmessage。 如果有需求，还可以传递参数。
//	[webView stringByEvaluatingJavaScriptFromString:@"showmessage('参数')"];
	// 若JS有返回值此方法会直接返回。返回的格式为URL编码格式，所以必须解码才能使用。 下边是解码方法
	//例如： NSString *data = [myWebView stringByEvaluatingJavaScriptFromString:@"showmessage('参数')"];
}

// URL 解码
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
	NSMutableString *outputStr = [NSMutableString stringWithString:input];
	[outputStr replaceOccurrencesOfString:@"+"
							   withString:@" "
								  options:NSLiteralSearch
									range:NSMakeRange(0, [outputStr length])];
	
	return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex  == 0)
	{
		UIImagePickerControllerSourceType sourceType=UIImagePickerControllerSourceTypeCamera;
		
		if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
			sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
		}
		UIImagePickerController * picker = [[UIImagePickerController alloc]init];
		picker.delegate = self;
		picker.allowsEditing = YES;
		picker.sourceType = sourceType;
		[self.navigationController presentViewController:picker animated:YES completion:nil];
		[picker release];
	}
	else if(buttonIndex == 1)
	{
		//Get from photo Ablum.
		UIImagePickerControllerSourceType sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
		
		if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
			sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
		}
		UIImagePickerController * picker = [[UIImagePickerController alloc]init];
		picker.delegate = self;
		picker.allowsEditing = YES;
		picker.sourceType  =sourceType;
		[self.navigationController presentViewController:picker animated:YES completion:nil];
		[picker release];
	}
	[self.actionSheetImpl.window removeGestureRecognizer:self.tapForSheet];
	self.actionSheetImpl = nil;
}
- (void)alertView:(HOAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [super alertView:alertView clickedButtonAtIndex:buttonIndex];
    if(alertView.tag == [kHO_DIALOG_NeedFillInInfo integerValue])
	{
		isPopedUpRemindDialog = NO;
		if(buttonIndex == 1)
		{
			[self viewProfile];
		}
	}
	
	if([alertView isKindOfClass:[HOAlertView class]])
	{
		[((HOAlertView *)alertView) alertDidClose];
	}
}

- (void)takePhoto:(id)sender
{
	if(self.actionSheetImpl == nil)
	{
		self.actionSheetImpl = [[UIActionSheet alloc]
								initWithTitle:NSLocalizedString(@"更新头像", @"更新头像")
								delegate:self
								cancelButtonTitle:@"取消"
								destructiveButtonTitle:@"拍照"
								otherButtonTitles:@"从相册选择",nil];
		self.actionSheetImpl.tag = 1115;
	}
	
	self.tapForSheet = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapFrom:)];
	self.tapForSheet.cancelsTouchesInView = NO;
	self.tapForSheet.delegate = self;
	[self.actionSheetImpl.window addGestureRecognizer:self.tapForSheet];
	self.actionSheetImpl.delegate = self;
	[self.actionSheetImpl showInView:self.view];
}

- (void)handleTapFrom:(UITapGestureRecognizer *)gesture
{
	if(self.actionSheetImpl != nil)
	{
		[self.actionSheetImpl dismissWithClickedButtonIndex:3 animated:YES];
		self.actionSheetImpl = nil;
		
	}
	
	[self showPickerView:NO];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
	NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
	[picker dismissViewControllerAnimated:YES completion:nil];
	UIImage *originImage = [info objectForKey:UIImagePickerControllerOriginalImage];
	
	NSData *data;
	//Maybe blow code run on the thrid thread for user experience.
	if (UIImagePNGRepresentation(originImage) == nil) {
		//Convert jpg image to NSData
		data = UIImageJPEGRepresentation(originImage, 0.1);
		CGFloat size = data.length / 1024;
		if(size >= 1024)
		{
			[self popupHintView:NSLocalizedString(@"头像大小不能超过1M，请重新上传!", @"头像大小不能超过1M，请重新上传!")];
			return;
		}
	} else {
		//Convert png image to NSData
		CGFloat size = 0;
		originImage = [HOUtils imageCompressForWidth:originImage targetWidth:200];
		data = UIImagePNGRepresentation(originImage);
		size = data.length / 1024;
		if(size >= 1024)
		{
			[self popupHintView:NSLocalizedString(@"头像大小不能超过1M，请重新上传!", @"头像大小不能超过1M，请重新上传!")];
			return;
		}
	}
	
	
	UIImage *img = [UIImage imageWithData:data];
	NSString *imageStr = [GTMBase64 stringByEncodingData:data];
	if(imageStr == nil)
	{
		[self popupHintView:NSLocalizedString(@"请上传有效的头像!", @"请上传有效的身份证头像!")];
		return;
	}
	if(img != nil)
	{
		self.defaultImg = img;
	}
	[headerImgView setImage:self.defaultImg];
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_UpdateFace,
										kHO_FIELD_ImageData:imageStr}];
//	[baseTableview reloadData];
	[pool release];
	
}

- (void)gotoMyReport
{
	
	self.tabBarController.tabBar.hidden = YES;
	[HOStaticDataMgr sharedInstance].needGotoMyReport = NO;
	HOMyReportViewController *viewCtrl = [[HOMyReportViewController alloc] init];
	viewCtrl.currentStatus = HO_REPORT_ORDER_ALL;
	viewCtrl.navTitle = NSLocalizedString(@"我的报修单", @"我的报修单");
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)gotoMyAssistantReport
{
	[HOStaticDataMgr sharedInstance].needGotoMyReport = NO;
	HOMyReportViewController *viewCtrl = [[HOMyReportViewController alloc] init];
	viewCtrl.isAssistantReport = YES;
	if([[HOStaticDataMgr sharedInstance] isDirectorUser])
	{
		viewCtrl.currentStatus = HO_REPORT_ORDER_UNACCEPT;
	}
	else
	{
		viewCtrl.currentStatus = HO_REPORT_ORDER_ACCEPTED;
	}
	
	viewCtrl.navTitle = NSLocalizedString(@"我的协助单", @"我的协助单");
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)gotoUnacceptAssistantReport
{
	[HOStaticDataMgr sharedInstance].needGotoMyReport = NO;
	HOMyReportViewController *viewCtrl = [[HOMyReportViewController alloc] init];
	viewCtrl.isAssistantReport = YES;
	viewCtrl.isUnacceptReport = YES;
	viewCtrl.currentStatus = HO_REPORT_ORDER_UNACCEPT;
	viewCtrl.navTitle = NSLocalizedString(@"未受理的协助单", @"未受理的协助单");
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)gotoMyExtraAssistantReport
{
	HOMyExtraAssistantViewController *viewCtrl = [[HOMyExtraAssistantViewController alloc] init];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)gotoMyMessage
{
	//我的消息
	[HOStaticDataMgr sharedInstance].needGotoMyMessage = NO;
	HOMessageViewController *viewCtrl = [[HOMessageViewController alloc] initWithModule:HO_MODULE_TYPE_MESSAGE];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)didChangeSwitch:(UISwitch *)sender
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_ChangeWorkStatus,
								  kHO_FIELD_Value:self.userInfo.isWorking? @"2" : @"1"}];
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
    [super didReceiveCommand:action data:data];
    if([action isEqualToString:kHO_ACTION_Logout])
    {
		dontNeedRequest = NO;
        isGotUserName = NO;
        [baseTableview reloadData];
    }
	else if([action isEqualToString:kHO_CMD_ReceiveNotificationMessage])
	{
		int type = [[data valueForKey:kHO_FIELD_SubType] intValue];
		if(type == HO_NC_TYPE_GRADE_CHECKING)
		{
			[self requestUserData];
		}
	}
	else if([action isEqualToString:kHO_CMD_TakeReport])
	{
		[self popupHintView:@"接单成功!"];
		[self goAcceptList];
	}
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
    if([response isEqualAction:kHO_ACTION_GetUser])
    {
		[self.userInfo parseDic:response.dataNode];
		int count = self.userInfo.unacceptTotalCount + self.userInfo.acceptedTotalCount;
		self.unreadMsgImgView.hidden = (count == 0);
		[baseTableview reloadData];
    }
	else if([response isEqualAction:kHO_ACTION_UpdateFace])
	{
		[self popupHintView:NSLocalizedString(@"头像更新成功!", @"头像更新成功!")];
		dontNeedRequest = NO;
		[self requestUserData];
	}
	else if([response isEqualAction:kHO_ACTION_ChangeWorkStatus])
	{
		[self requestUserData];
	}
	else if([response isEqualAction:kHO_ACTION_ChangeToken])
	{
		NSString *token = [response.dataNode objectForKey:kHO_FIELD_Token];
		[[HOUIMgr sharedInstance] saveToken:token];
		[self requestUserData];
	}
	else
	{
		
	}
}

- (void)dealloc
{
	self.defaultImg = nil;
	[HOUtils removeFromSuperViewForViews:headerImgView,
										nameLbl,
										mobileLbl,
										genderImgView];
	[headerImgView release];
	[nameLbl release];
	[mobileLbl release];
	[genderImgView release];
    [super dealloc];
}

#pragma mark
@end
